/*******************************************************************************
 * Copyright (c) 2014-6-28 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package org.iff.infra.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * <pre>
 * Usage: add "Authorization" to http header.
 * setHeader("Authorization",ipsMd5())
 * </pre>
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-6-28
 */
public class HttpHelper {
	public static Map<String, InetAddress> getAddress() {
		Map<String, InetAddress> map = new HashMap<String, InetAddress>();
		try {
			for (Enumeration<NetworkInterface> interfaces = NetworkInterface
					.getNetworkInterfaces(); interfaces.hasMoreElements();) {
				NetworkInterface networkInterface = interfaces.nextElement();
				if (networkInterface.isLoopback()
						|| networkInterface.isVirtual()
						|| !networkInterface.isUp()) {
					continue;
				}
				Enumeration<InetAddress> addresses = networkInterface
						.getInetAddresses();
				if (addresses.hasMoreElements()) {
					InetAddress inetAddress = addresses.nextElement();
					map.put(inetAddress.getHostAddress(), inetAddress);
				}
			}
		} catch (SocketException e) {
			System.out.println("Error when getting host ip address: <{}>."
					+ e.getMessage());
		}
		return map;
	}

	public static String ipsMd5() {
		StringBuilder sb = new StringBuilder(256);
		Map<String, InetAddress> address = getAddress();
		for (Entry<String, InetAddress> entry : address.entrySet()) {
			String md5 = md5(entry.getKey());
			md5 = StringHelper.reverse(md5);
			md5 = md5(md5);
			sb.append(md5);
		}
		return sb.toString();
	}

	public static String ipMd5(String ipAddress) {
		StringBuilder sb = new StringBuilder(64);
		String md5 = md5(ipAddress);
		md5 = StringHelper.reverse(md5);
		md5 = md5(md5);
		sb.append(md5);
		return sb.toString();
	}

	public static boolean validateIpMd5(String ip, String ipsMd5) {
		return ip != null && ipsMd5 != null && ipsMd5.indexOf(ipMd5(ip)) > -1;
	}

	public static String md5(String inStr) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			Logger.error("[MessageDigest.getInstance(\"MD5\")]", e);
			return "";
		}
		char[] charArray = inStr.toCharArray();
		byte[] byteArray = new byte[charArray.length];

		for (int i = 0; i < charArray.length; i++) {
			byteArray[i] = (byte) charArray[i];
		}
		byte[] md5Bytes = md5.digest(byteArray);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = ((int) md5Bytes[i]) & 0xff;
			if (val < 16) {
				hexValue.append('0');
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();

	}
}
